const mongoose = require('mongoose');
const User = require('./src/models/user.model');
require('dotenv').config();

async function createAdmin() {
  try {
    console.log('连接数据库...');
    await mongoose.connect(process.env.MONGODB_URI);
    console.log('数据库连接成功');
    
    // 检查是否已存在管理员用户
    let admin = await User.findOne({ name: 'admin' });
    if (admin) {
      console.log('管理员用户已存在');
      console.log('用户名: admin');
      console.log('密码: admin123');
      process.exit(0);
    }
    
    // 创建管理员用户
    admin = await User.create({
      name: 'admin',
      email: 'admin@trove.com',
      password: 'admin123',
      role: 'admin',
      isActive: true,
      isVerified: true
    });
    
    console.log('管理员用户创建成功!');
    console.log('用户名: admin');
    console.log('密码: admin123');
    console.log('邮箱: admin@trove.com');
    process.exit(0);
  } catch (error) {
    console.error('创建管理员用户失败:', error);
    process.exit(1);
  }
}

createAdmin();
